<template>
  <div class="home">
    <nav>
      <van-tabs
        class="tabs"
        @click="onChannelChange"
      >
        <van-tab
          v-for="item in channelList"
          :key="item.id"
          :title="item.name"
          :name="item.id"
        />
      </van-tabs>
      <div class="icon-wrapper">
        <van-icon
          name="search"
          size="20"
          @click="onClickSearch"
        />
      </div>
      <div class="icon-wrapper">
        <van-icon
          name="ellipsis"
          size="20"
        />
      </div>
    </nav>
    <!-- 文章模块 -->
    <main>
      <div
        v-if="flag"
        class="loading"
      >
        <van-loading type="spinner" />
        loading
      </div>
      <van-empty
        v-else-if="!articleList"
        image="error"
        description="请求错误"
      />
      <div
        class="empty"
        v-else-if="!articleList.length"
      >
        暂无数据
      </div>
      <article-list
        v-else
        :data="articleList"
      />
    </main>
  </div>
</template>

<script>
import {
  mapState,
  mapActions,
} from 'vuex';

export default {
  name: 'HomePage',
  data() {
    return {
      flag: true,
    };
  },
  computed: {
    // 集中导入model层数据
    ...mapState('channel', [
      'channelList',
    ]),
    ...mapState('article', [
      'articleList',
    ]),
  },
  methods: {
    // 频道
    ...mapActions('channel', [
      'updateChannelList'
    ]),
    // 文章
    ...mapActions('article', [
      'updateArticleList'
    ]),
    ...mapActions('article', [
      'updateDetailsList'
    ]),
    async onFlag(id) {
      this.flag = true;
      await this.updateArticleList(id);
      this.flag = false;
    },
    onChannelChange(id) {
      this.onFlag(id);
    },
    onClickSearch() {
      this.$router.push('search')

    }
  },
  created() {
    this.updateChannelList();
    this.onFlag(0);
  },

}
</script>

<style lang="less" scoped>
@import url('@/assets/style/common.less');

.home {
  nav {
    display: flex;
    width: 100%;
    align-items: center;
    justify-content: space-around;
    box-shadow: 0 3px 3px #e9e9e9;

    .tabs {
      flex: 1;
      overflow: hidden;
      position: relative;

      &::after {
        content: '';
        display: block;
        height: 100%;
        width: 40px;
        background: linear-gradient(to right, transparent, white);
        position: absolute;
        top: 0;
        right: 0;
      }
    }

    .icon-wrapper {
      margin: 8px;

      &:last-child {
        margin-right: 16px;
      }
    }
  }

  main {
    padding: 0 8px;
  }
}
.loading {
  color: #aaa;
  width: 100%;
  text-align: center;
  margin-top: 100px;
}
.empty{
  color: #aaa;
  width: 100%;
  text-align: center;
  margin-top: 100px;
}
</style>
